<?php
    @header("Content-type:text/html;chaset=utf-8");
    @include_once("conn.php");
       
    $obj = array();

    $key = $_GET["key"];
    $orderCol = $_GET["orderCol"];
    $orderType = $_GET["orderType"];
    $pageNum = $_GET["pageNum"];
    $showNum = $_GET["showNum"]*1;
    
    if(!($orderCol&&$orderType)){
        $obj["status"] = false;
        $obj["detail"] = "请输入完整的参数";
        exit(json_encode($obj));
    }

    $sql = "select count(*) as total from `goodslist` where goodsName like '%$key%'";
    
    $result = mysqli_query($conn,$sql);

    $item = mysqli_fetch_assoc($result);

    if($item){
        $obj["status"] = true;
        $obj["detail"] = "succeed";
        $obj["total"] = $item["total"];
    }else{
        $obj["status"] = false;
        $obj["detail"] = "fail";
    }

    $maxPage =ceil( $item["total"]/$showNum);


    if($pageNum<=1){
        $pageNum = 1;
    }

    if($pageNum>=$maxPage){
        $pageNum = $maxPage;
    }


    $pageNum = ($pageNum-1)*$showNum;
    
    $sql = "select id,goodsId,goodsName,goodsImg,goodsPrice from `goodslist` where goodsName like '%$key%' order by $orderCol $orderType limit $pageNum,$showNum";
    
    $result = mysqli_query($conn,$sql);
    
    
    $all = array();
    while($item = mysqli_fetch_assoc($result)){
        //数据预处理
        array_push($all,$item);
    }
    
    // print_r($item);
    
    if (count($all)) {
        $obj["status"] = true;
        $obj["detail"] = "succeed";
        $obj["list"] = $all;
        $obj["maxPage"] = $maxPage;
    }else{
        $obj["status"] = false;
        $obj["detail"] = "fail";
        $obj["sql"] = $sql;
    }

    echo json_encode($obj);

?>